/**
 * @author LKQ
 * @date 2022/1/1 11:05
 * @description 深度优先搜索（Depth-First-Search)，利用函数自身递归
 */
public class Solution {
    public static void main(String[] args) {

    }

    public int maxAreaOfIsland(int[][] grid) {
        int max = 0;
        for(int i = 0;i < grid.length;i++){
            for(int j = 0;j < grid[i].length;j++){
                areaOfIsland(grid,i,j);
                max = Math.max(max,area);
                area = 0;
            }
        }
        return max;
    }

    static int area = 0;

    public static void areaOfIsland(int[][] grid,int i,int j){
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) {
            return;
        }
        if(grid[i][j] == 0) {
            return;
        }
        area++;
        grid[i][j] = 0;
        areaOfIsland(grid,i + 1,j);
        areaOfIsland(grid,i,j + 1);
        areaOfIsland(grid,i - 1,j);
        areaOfIsland(grid,i,j - 1);
    }
}
